<%--
    Document   : CustomerCheckOut
    Created on : Nov 22, 2012, 11:26:08 PM
    Author     : vupttse
--%>

<%@page import="assignment2.stub.OrderDetailsTbl_Stub"%>
<%@page import="assignment2.data.OrderDetails"%>
<%@page import="assignment2.data.Customers"%>
<%@page import="assignment2.stub.CustomerTbl_Stub"%>
<%@page import="assignment2.data.Orders"%>
<%@page import="assignment2.stub.OrderTbl_Stub"%>
<%@page import="assignment2.stub.ProductsTbl_Stub"%>
<%@page import="assignment2.data.Products"%>
<%@page import="assignment2.data.Users"%>
<%@page import="assignment2.stub.Order"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Customer Checkout!</title>
        <link href="css/modern.css" rel="stylesheet">
        <link href="css/modern-responsive.css" rel="stylesheet">
        <link href="css/site.css" rel="stylesheet" type="text/css">

        <script src="js/jquery-1.8.2.min.js"></script>
        <script src="js/google-analytics.js"></script>
        <script src="js/github.info.js"></script>
        <script src="js/sharrre/jquery.sharrre-1.3.4.min.js"></script>
        <script src="js/carousel.js"></script>
        <script src="js/dropdown.js"></script>
        <script src="js/slider.js"></script>
        <script src="js/hover.js"></script>
        <script src="js/pagecontrol.js"></script>
        <script src="js/accordion.js"></script>
    </head>
    <body>
        <%
            Object ob = session.getAttribute("orders");
            Date date = new Date();

            java.sql.Date sqlDate = new java.sql.Date(date.getTime());
            if (ob == null) {
                response.sendRedirect("ViewProduct.jsp");
            } else {
                Collection<Order> orders = (Collection<Order>) session.getAttribute("orders");
                Iterator<Order> it = orders.iterator();
                Users User = (Users) session.getAttribute("currentUser");
                ProductsTbl_Stub pts = new ProductsTbl_Stub();
                OrderTbl_Stub ots = new OrderTbl_Stub();
                OrderDetailsTbl_Stub odts = new OrderDetailsTbl_Stub();
                CustomerTbl_Stub cts = new CustomerTbl_Stub();
                while (it.hasNext()) {
                    Order order = it.next();
                    Products entity = pts.findByID(order.getProductId());
                    Customers customers = cts.searchByUsername(User);
                    Orders orderEntity = new Orders(customers.getCustomerID());
                    ots.insert(orderEntity);
                    OrderDetails detailEntity = new OrderDetails(ots.getOrderID(), order.getQuantity(), 0.0, order.getQuantity() * entity.getUnitPrice(), "Pending", sqlDate.toString(), entity);
                    odts.insert(detailEntity);
                }
            }

        %>
        <div class="page">
            <jsp:include page="Navigation.jsp"></jsp:include>
                <div class="page-region">
                    <div class="page-region-content" style="min-height: 500px">
                        <div class="span8" style="margin-left: 20px">
                            <h1>Your orders have been made!</h1>
                            <a href="CartPage.jsp" class="button">View shopping cart!</a>
                            <a href="ViewProduct.jsp" class="button">Continue shopping!</a>
                        </div>
                    </div>
                </div>
            <%@include  file="footer.html" %>
        </div>
    </body>
</html>
